package com.lcj.bff.customer.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.lcj.bff.customer.controller.form.MessageIdForm;
import com.lcj.bff.customer.controller.form.MessageUserIdAndIdentifyForm;
import com.lcj.bff.customer.controller.form.SearchMessageByPageForm;
import com.lcj.bff.customer.service.MessageService;
import com.lcj.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/message")
@Tag(name = "MessageController", description = "消息模块网络接口")
public class MessageController {

    @Resource
    private MessageService messageService;

    @PostMapping("/receiveBillMessage")
    @SaCheckLogin
    @Operation(summary = "同步接收新订单消息")
    public Result receiveBillMessage(@RequestBody MessageUserIdAndIdentifyForm form){
        long customerId = StpUtil.getLoginIdAsLong();
        form.setUserId(customerId);
        form.setIdentity("customer_bill");
        String msg = messageService.receiveBillMessage(form);
        return Result.ok().put("result",msg);
    }

    @PostMapping("/searchMessageByPage")
    @Operation(description = "分页查询消息")
    @SaCheckLogin
    public Result searchMessageByPage(@RequestBody @Valid SearchMessageByPageForm form) {
        form.setUserId(StpUtil.getLoginIdAsLong());
        form.setIdentity("customer");
        form.setStart((form.getStart()-1) * form.getLength());
        Result result = messageService.searchMessageByPage(form);
        return result;
    }

    @PostMapping("/searchMessageById")
    @Operation(summary = "根据ID查询消息")
    @SaCheckLogin
    public Result searchMessageById(@Valid @RequestBody MessageIdForm form) {
        Result result = messageService.searchMessageById(form);
        return result;
    }

    @PostMapping("/updateUnreadMessage")
    @Operation(summary = "未读消息更新成已读消息")
    @SaCheckLogin
    public Result updateUnreadMessage(@Valid @RequestBody MessageIdForm form) {
        Result result = messageService.updateUnreadMessage(form);
        return result;
    }

    @PostMapping("/deleteMessageRefById")
    @Operation(summary = "删除消息")
    @SaCheckLogin
    public Result deleteMessageRefById(@Valid @RequestBody MessageIdForm form) {
        Result result = messageService.deleteMessageRefById(form);
        return result;
    }
}